const database = require("../app/database");

class UserServices {
  //查询当前用户收藏的所有文章的id，点赞的所有评论的id，点赞的所有留言的id
  async getallcollectessayid(id) {
    //获取收藏的所有文章的id
    const str1 = `
			select essay_id from collection where user_id=?;
		`;
    const [result1] = await database.execute(str1, [id]);
    let arr1 = [];
    result1.forEach((item) => {
      arr1.push(item.essay_id);
    });

    //获取点赞的所有评论的id
    const str2 = `
      select comment_id from commentthumb where user_id=?;
    `;
    const [result2] = await database.execute(str2, [id]);
    let arr2 = [];
    result2.forEach((item) => {
      arr2.push(item.comment_id);
    });

    //获取点赞的所有评论的id
    const str3 = `
      select message_id from messagethumb where user_id=?;
    `;
    const [result3] = await database.execute(str3, [id]);
    let arr3 = [];
    result3.forEach((item) => {
      arr3.push(item.message_id);
    });
    return {
      status: 200,
      result: {
        collectarr: arr1,
        commentarr: arr2,
        messagearr: arr3,
      },
    };
  }
  //注册
  async register(body) {
    const { username, password, sex, birthday, introduction, avatar } = body;
    //先看用户名是否已经存在
    const str1 = `
      select * from user where username=?
    `;
    const [result1] = await database.execute(str1, [username]);
    if (result1.length > 0) {
      return {
        status: 403,
        message: "用户名已经存在",
      };
    }
    const str2 = `
      insert into user( username, password, sex, birthday, introduction,avatar) values (?,?,?,?,?,?)
    `;
    await database.execute(str2, [
      username,
      password,
      sex,
      birthday,
      introduction,
      avatar,
    ]);
    return {
      status: 200,
      message: "注册成功",
    };
  }
  //登录
  async login(body) {
    const { username, password } = body;
    const str = ` 
      select * from user where username=? and password=?
    `;
    const [result] = await database.execute(str, [username, password]);
    if (result.length === 0) {
      return {
        status: 401,
        message: "用户名或密码错误",
      };
    }

    return {
      status: 200,
      user: result[0],
      message: "登陆成功",
    };
  }

  //获取所有用户信息（包含搜索）
  async getallusers(start, size, search) {
    let str;
    if (start === -1 && size == -1) {
      str = `
        select * from user
      `;
      const [result] = await database.execute(str);
      return {
        status: 200,
        result,
      };
    } else {
      str = `
        select * from user where username like '%${search}%' limit ?,?
      `;
      const [all] = await database.execute(
        `select * from user where username like '%${search}%'`
      );
      const [result] = await database.execute(str, [start, size]);
      return {
        status: 200,
        result,
        allcount: all.length,
      };
    }
  }

  //用户个人成就
  async accomplish(id) {
    //文章被收藏数量
    const str1 = `
      select count(*) collectcount from collection where essay_id in (select id from essay where essay.user_id=?);
    `;
    const [[{ collectcount }]] = await database.execute(str1, [id]);

    //评论数量
    const str2 = `
      select count(*) commentcount from comments where user_id=?
    `;
    const [[{ commentcount }]] = await database.execute(str2, [id]);

    //文章字数
    const str3 = `
      select content from essay where user_id=?
    `;
    const [re3] = await database.execute(str3, [id]);
    // let essayfontcount = re3.reduce((a, b) => a.content + b.content, "").length;
    let essayfontcount = 0;
    re3.forEach((item) => {
      essayfontcount += item.content.length;
    });

    //文章被阅读量
    const str4 = `
      select views from essay where user_id=?
    `;
    const [re4] = await database.execute(str4, [id]);
    const views = re4.reduce((a, b) => a + b.views, 0);
    return {
      status: 200,
      result: {
        collectcount,
        commentcount,
        essayfontcount,
        views,
      },
    };
  }

  //更新用户数据
  async updateuser(id, body) {
    const { username, avatar, sex, birthday, introduction } = body;
    const str = `
      update user set username=?,avatar=?,introduction=?,sex=?,birthday=? where id=?;
    `;
    await database.execute(str, [
      username,
      avatar,
      introduction,
      sex,
      birthday,
      id,
    ]);
    return {
      status: 200,
      result: {
        message: "修改成功",
      },
    };
  }

  //根据id重新获取用户信息
  async getoneuser(id) {
    const str = `
      select * from user where id=?
    `;
    const [[result]] = await database.execute(str, [id]);
    return {
      status: 200,
      result,
    };
  }

  //gitee三方登录
  async giteelogin(user) {
    const { id, name, avatar_url } = user;
    //先查询有没有登陆过
    const str1 = `
      select * from user where id=?
    `;
    const [re1] = await database.execute(str1, [id]);
    if (re1.length > 0) {
      //如果登陆过，就更新一次，避免用户数据不是最新的
      const str3 = `
        update user set username=?,sex=?,birthday=?,introduction=?,avatar=? where id=?;
      `;
      await database.execute(str3, [
        name,
        0,
        "2022-06-21 12:34:32",
        "|",
        avatar_url,
        id,
      ]);
      return {
        status: 200,
        result: re1[0],
      };
    }
    const str2 = `
      insert into user( id, username, password, sex, birthday, introduction,avatar) values (?,?,?,?,?,?,?)
    `;
    await database.execute(str2, [
      id,
      name,
      "MIGfMA0GCSqGSIb3D",
      0,
      "2022-06-21 12:34:32",
      "|",
      avatar_url,
    ]);
  }

  //根据giteeid获取用户信息
  async bygitee(id) {
    const str = `
      select * from user where id=?
    `;
    const [[user]] = await database.execute(str, [id]);
    return {
      status: 200,
      result: {
        user,
      },
    };
  }

  //管理员登录
  async mangelogin(body) {
    const { username, password } = body;
    const str = `
      select * from manage where username=? and password=?
    `;
    const [result] = await database.execute(str, [username, password]);
    if (result.length === 0)
      return {
        status: 401,
        result: {
          message: "用户名或密码错误",
        },
      };
    return {
      status: 200,
      result: {
        user: result[0],
      },
    };
  }

  //删除用户
  async deleteone(id) {
    const str = `
      delete from user where id=?
    `;
    await database.execute(str, [id]);
    return {
      status: 200,
      result: {
        message: "删除成功",
      },
    };
  }
}

module.exports = new UserServices();
